use std::env;
use std::process;

use minigrep::Config;

// 国内 NTP https://dns.iui.im/ntp/
fn main() {

    // The env::args function return an iterator! Rather than colllecting the 
    // iterator values into a vector and then passing a slice to Config::build,
    // now we're passing ownership of the iterator returned from env::args to 
    // Config::build directly.
    let config = Config::build(env::args()).unwrap_or_else(|err| {
        eprintln!("解析命令行时出错。{err}");
        process::exit(1);
    });

    /* We use if let rather than unwrap_or_else to check whether run returns
     * an Err value and call process::exit(1) if it does. The run function
     * doesn't return a value that we want to unwrap in the smae way that
     * Config::build() returns the Config instance. Because run returns ()
     * in the succcess case, we only care about detecting an error, so we
     * don't need unwrap_or_else to return the unwrapped value, which would
     * only be ().
     */
    if let Err(e) = minigrep::run(config) {
        eprintln!("应用程序错误：{e}");
    }
}

